package cn.edu.ecut.scheduling;

/**
 * 1、java.lang.Thread 类实现了 java.lang.Runnable接口
 * 2、通过继承Thread重写run方法实现线程要完成的操作
 * 3、可以通过 Thread 类的实例方法 setPriority方法来设置线程优先级
 * 4、优先级高的线程优先获得CPU时间片，优先执行
 */
public class PriorityThread extends Thread {
	
	public PriorityThread(String name) {
		super(name);
	}

	@Override
	public void run() {
		for (int i = 1; i < 11 ; i++) {
			System.out.println( Thread.currentThread() + " : " + i );
			try {
				Thread.sleep( 1 ); // 当前线程睡眠
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		
		Thread f = new PriorityThread( "first" );
		f.setPriority( Thread.NORM_PRIORITY - 2 );
		
		Thread s = new PriorityThread( "second" );
		s.setPriority( Thread.NORM_PRIORITY + 2 );
		
		Thread t = new PriorityThread( "third" );
		t.setPriority( Thread.MAX_PRIORITY );
		
		f.start();
		s.start();
		t.start();
		
	}
	
}
